home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0022_File and Record Locks.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  3KB  |  78 lines

  1. {
  2. MARCO MILTENBURG
  3.  
  4. > Currently I'm writing a Program which must be able to handle multitask
  5. > evironments. But as I'm trying to Write a Record which is open in another
  6. > Window (of DesqView for instance) than a runtime error 5 appears. Seems
  7. > logical. But how do I 'lock' the Record, what are the attibutes, and what
  8. > must the Program do if it can't open a Record???
  9.  
  10.       Locking isn't that difficult... First of all, do you have to keep the
  11. File available For anybody else (in another task) or not. if not, use the
  12. Filesharing bits when you're opening the File. They are :
  13.  
  14. bit 0-2   = 000 - read permission For your own appliction
  15.             001 - Write permission For you own application
  16.             010 - both read and Write permission For you own application
  17.  
  18. bit 3     = 0   - Always zero!
  19.  
  20. bit 4-6   = 000 - compatibilty mode. Share the File whenever possible.
  21.             001 - reading and writing not allowed For other applications
  22.             010 - writing not allowed For other applications (usefull when
  23.                   you're gonna read the File, so others can not update it)
  24.             011 - reading not allowed For other applications (usefull when
  25.                   you're gonna update the File and others may not read it).
  26.             100 - Full access For other applications (dangerous in my point of
  27.                   view!).
  28.  
  29. bit 7     = 0   - Lower process owns File
  30.             1   - File only For current process.
  31.  
  32. Set the bits to your needs and assign the value to FileMode before opening the
  33. File. For example, I want to read a File which must be locked completly. Is
  34. must use the value 00010000b which is $10. So use FileMode = $10 before opening
  35. the File. Please note that FileMode only take affect on Files which are
  36. declared as ': File' or ': File of ....'. It's not supported on ': Text' Files.
  37. if you want to lock these Files, use the next method.
  38.  
  39. if you only want to lock a single Record of a File (or an entier File) you can
  40. use the following Function :
  41.  
  42.  
  43. Ooh BTW: This will only work With Dos 3.0+ (of course ;-) With SHARE loaded.
  44. }
  45.  
  46. Function FileLocking(Action     : Byte;
  47.                      Handle     : Word;
  48.                      Start, end : LongInt) : Boolean;
  49. Var
  50.   Regs : Registers;
  51. begin
  52.   Regs.AH := $5C;
  53.   Regs.AL := Action;
  54.   Regs.BX := Handle;
  55.   Regs.CX := Hi(Start);
  56.   Regs.DX := Lo(Start);
  57.   Regs.DI := Lo(end);
  58.   Regs.SI := Hi(end);
  59.   Intr($21, Regs);
  60.   FileLocking := ((Regs.FLAGS and $01) = 0);
  61. end;
  62.  
  63. {
  64. Use For Action '0' to lock or '1' to unlock the File. The funtion returns True
  65. when succesfull. The Handle Variable must contain the Filehandle, assigned by
  66. Dos. For TextFiles you can obtain this handle With :
  67.  
  68.   TextRec(T).Handle
  69.  
  70. where T is the TextFile (declared With T : Text). I don't know how to obtain
  71. the Filehandle of another FileType at the moment. I will have to look For it.
  72. Start and end contain the starting and ending position (in Bytes) from what you
  73. want to lock (for Typed Files, they can easaly be calculated using FilePos and
  74. SizeOf(....Record) etc..). if you want to lock the entire File, use 0 For start
  75. and $FFFFFFFF For end. Locking beyond the end of the File doesn't result in an
  76. error!
  77. }
  78.